﻿using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.swpublished;
using System;

namespace Demo
{
    public class UserPMPForOpenHole : IDisposable
    {
        //Local Objects
        public PropertyManagerPage2 swPropertyPage = null;

        private PMPHandlerForOpenHole handler = null;

        private SldWorks iSwApp = null;

        #region Controls

        private PropertyManagerPageGroup groupOpenHole;

        private PropertyManagerPageLabel labelHoleFaceBase;
        private PropertyManagerPageLabel labelOpenHoleFace;

        private int labelHoleFaceBaseId = 101;
        private int labelOpenHoleFaceId = 102;

        public const int group1ID = 0;

        public UserPMPForOpenHole(SldWorks swApp)
        {
            iSwApp = swApp;
            CreatePropertyManagerPage();
        }

        private void CreatePropertyManagerPage()
        {
            int errors = -1;

            int options = (int)swPropertyManagerPageOptions_e.swPropertyManagerOptions_OkayButton |
                          (int)swPropertyManagerPageOptions_e.swPropertyManagerOptions_CancelButton;

            handler = new PMPHandlerForOpenHole(iSwApp, this);

            swPropertyPage = iSwApp.ICreatePropertyManagerPage("开孔向导", options, handler, ref errors);

            if (swPropertyPage != null && errors == (int)swPropertyManagerPageStatus_e.swPropertyManagerPage_Okay)
            {
                try
                {
                    AddControls();
                }
                catch (Exception e)
                {
                    iSwApp.SendMsgToUser2(e.Message, 0, 0);
                }
            }
        }

        // 孔基准面选择器
        public IPropertyManagerPageSelectionbox selectionFaceBase;

        public const int seletionFaceBaseId = 201;

        // 开孔目标面选择器
        public IPropertyManagerPageSelectionbox seletionFaceTarget;

        public const int seletionFaceTargetId = 202;
        IPropertyManagerPageCheckbox checkBoxSlotRotaion;
        public const int checkBoxSlotRotaionId = 203;


        private void AddControls()
        {
            ModelDoc2 actvieModelDoc = iSwApp.IActiveDoc2;

            short controType = -1;
            short align = -1;
            var options = -1;
            bool retval;

            //Add Message
            //retval = swPropertyPage.SetMessage3("步骤:\r\n 1.选择孔基准面 \r\n 2.选择要开孔的面 \r\n",
            //                                (int)swPropertyManagerPageMessageVisibility.swImportantMessageBox,
            //                                (int)swPropertyManagerPageMessageExpanded.swMessageBoxExpand,
            //                                "孔映射向导");

            options = (int)swAddGroupBoxOptions_e.swGroupBoxOptions_Visible |
                (int)swAddGroupBoxOptions_e.swGroupBoxOptions_Expanded;

            groupOpenHole = (PropertyManagerPageGroup)swPropertyPage.AddGroupBox(group1ID, "映射孔向导", options);

            #region 孔基准面选择器

            controType = (int)swPropertyManagerPageControlType_e.swControlType_Label;
            align = (int)swPropertyManagerPageControlLeftAlign_e.swControlAlign_LeftEdge;
            options = (int)swAddControlOptions_e.swControlOptions_Enabled |
                      (int)swAddControlOptions_e.swControlOptions_Visible;

            labelHoleFaceBase = (PropertyManagerPageLabel)groupOpenHole.AddControl(labelHoleFaceBaseId, controType, "孔基准面", align, options, "请选择孔基准面");
            controType = (int)swPropertyManagerPageControlType_e.swControlType_Selectionbox;
            align = (int)swPropertyManagerPageControlLeftAlign_e.swControlAlign_LeftEdge;
            options = (int)swAddControlOptions_e.swControlOptions_Visible |
                        (int)swAddControlOptions_e.swControlOptions_Enabled;

            selectionFaceBase = (IPropertyManagerPageSelectionbox)groupOpenHole.AddControl2(seletionFaceBaseId, controType, "孔基准面", align, options, "选择孔基准面");

            if (selectionFaceBase != null)
            {
                int[] filter = { (int)swSelectType_e.swSelFACES };
                selectionFaceBase.Mark = 1;
                selectionFaceBase.SetSelectionFilters(filter);
                selectionFaceBase.SetSelectionColor(true, (int)swUserPreferenceIntegerValue_e.swSystemColorsSelectedItem1);
                selectionFaceBase.AllowMultipleSelectOfSameEntity = false;
                selectionFaceBase.SingleEntityOnly = false;
                selectionFaceBase.Height = 30;

                selectionFaceBase.SetCalloutLabel("孔映射基准面");
            }

            #endregion 孔基准面选择器

            #region 开孔面选择器

            // 添加标签
            controType = (int)swPropertyManagerPageControlType_e.swControlType_Label;
            align = (int)swPropertyManagerPageControlLeftAlign_e.swControlAlign_LeftEdge;
            options = (int)swAddControlOptions_e.swControlOptions_Enabled |
                      (int)swAddControlOptions_e.swControlOptions_Visible;
            labelOpenHoleFace = (PropertyManagerPageLabel)groupOpenHole.AddControl(labelOpenHoleFaceId, controType, "开孔面", align, options, "请选择开孔面");

            // 添加开孔面选择器
            controType = (int)swPropertyManagerPageControlType_e.swControlType_Selectionbox;
            align = (int)swPropertyManagerPageControlLeftAlign_e.swControlAlign_LeftEdge;
            options = (int)swAddControlOptions_e.swControlOptions_Visible |
                        (int)swAddControlOptions_e.swControlOptions_Enabled;

            seletionFaceTarget = (IPropertyManagerPageSelectionbox)groupOpenHole.AddControl2(seletionFaceTargetId, controType, "开孔面", align, options, "选择开孔面");

            if (seletionFaceTarget != null)
            {
                int[] filter = { (int)swSelectType_e.swSelFACES };
                seletionFaceTarget.Mark = 2;
                seletionFaceTarget.Height = 50;
                seletionFaceTarget.SetSelectionFilters(filter);
                seletionFaceTarget.SetSelectionColor(true, (int)swUserPreferenceIntegerValue_e.swAssemblyVisualizationComponentColor3);
                seletionFaceTarget.AllowMultipleSelectOfSameEntity = false;
                seletionFaceTarget.SingleEntityOnly = false;
                seletionFaceTarget.SetCalloutLabel("开孔面");
                seletionFaceTarget.Style = (int)swPropMgrPageSelectionBoxStyle_e.swPropMgrPageSelectionBoxStyle_WantListboxSelectionChanged;

            }

            #endregion 开孔面选择器


            #region  腰孔旋转90度CheckBox
            controType = (int)swPropertyManagerPageControlType_e.swControlType_Checkbox;
            align = (int)swPropertyManagerPageControlLeftAlign_e.swControlAlign_LeftEdge;
            options = (int)swAddControlOptions_e.swControlOptions_Enabled |
                    (int)swAddControlOptions_e.swControlOptions_Visible;

            checkBoxSlotRotaion = (IPropertyManagerPageCheckbox)groupOpenHole.AddControl2(checkBoxSlotRotaionId, controType, "腰孔旋转90°", align, options, "腰孔映射是否旋转90度");

            #endregion

            #region 保留映射关系


            #endregion
        }

        #endregion Controls

        public void Show()
        {
            if (swPropertyPage != null)
            {
                swPropertyPage.Show();
            }
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
}